#include "Manager.h"
#include <QSettings>
Cfg::Cfg()
{
}
extern "C" CFG_EXPORT bool AddAuthority(QString sLevel,QString auth)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value;
    key.append(",");
    key.append(auth);
    key.append(",");

    //user 三级权限
    //for(int i=0;i<3;i++)
    do{

        QVariant var;
        var =m_IniFile->value("System/"+sLevel);
        if(var.isNull())
            break;
        value = m_IniFile->value("System/"+sLevel).toString().trimmed();
        qDebug() << value;
        if(value.indexOf(key)>=0){
            ret = true;
        }
        else {
            value.append(key);
            var = QVariant(value);
            m_IniFile->setValue("System/"+sLevel,var);
            ret = true;
            m_IniFile->sync();
            break;
        }

    }while(false);


    delete m_IniFile;
    return ret;
}
extern "C" CFG_EXPORT bool DelAuthority(QString sLevel,QString auth)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value;
    key.append(",");
    key.append(auth);
    key.append(",");

    int pos;
    // 三级权限
    //for(int i=0;i<3;i++)
    do{

        QVariant var;
        var =m_IniFile->value("System/"+sLevel);
        if(var.isNull())
            break;
        value = m_IniFile->value("System/"+sLevel).toString().trimmed();
        qDebug() << value;
        pos=value.indexOf(key);
        if(pos >= 0){
            value.replace(pos,key.length(),"");
            var = QVariant(value);
            m_IniFile->setValue("System/"+sLevel,var);
            ret = true;
            m_IniFile->sync();
            break;
        }
        else
            qDebug() << "No haeve Authority:" << sLevel << auth;

    }while(false);


    delete m_IniFile;
    return ret;
}

extern "C" CFG_EXPORT bool CheckSetting(QString ObjName,QString user)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    int iLevel = -1;
    int iUser = -1;
    QString key,ukey;
    QString value;

    //user 三级权限
    for(int i=0;i<3;i++){

        QVariant var;
        key = QString("LEVEL%1").arg(i);
        var =m_IniFile->value("User/"+key);
        if(var.isNull())
            break;
        value = m_IniFile->value("User/"+key).toString().trimmed();
        qDebug()<<__FUNCTION__ << __LINE__ <<value;
        ukey="";
        ukey.append(0x01);
        ukey.append(user);
        ukey.append(0x02);
        if(value.indexOf(ukey)>=0){
            iUser =i;
            qDebug() << user << key;
            break;
        }

    }
    //ObjName 三级权限

    for(int i=0;i<3;i++){

        QVariant var;
        key = QString("LEVEL%1").arg(i);
        //qDebug() << key;
        var =m_IniFile->value("System/"+key);
        if(var.isNull())
            break;
        value = m_IniFile->value("System/"+key).toString().trimmed();
        qDebug() << key << value;
        if(value.indexOf(","+ObjName+",")>=0){
            iLevel =i;
            if(iLevel == iUser) //require for earch compare
                break;
        }
    }
    //
    if( iUser ==-1 || iLevel == -1){
        qDebug() << "iUser Or iLevel is -1";
        ret = false;
    }
    else if(iUser ==iLevel)
        ret = true;
    else
        ret = false;

    delete m_IniFile;
    return ret;
}

extern "C" CFG_EXPORT bool CheckUserPwd(QString user,QString pwd)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value;
    QString ukey;
    ukey.append(0x01);
    ukey.append(user);
    ukey.append(0x02);
    ukey.append(pwd);
    ukey.append(0x03);
    qDebug() << ukey;
    //user 三级权限
    for(int i=0;i<3;i++){

        QVariant var;
        key = QString("LEVEL%1").arg(i);
        var =m_IniFile->value("User/"+key);
        if(var.isNull())
            break;
        value = m_IniFile->value("User/"+key).toString();
        qDebug() << value;
        if(value.indexOf(ukey)>=0){
            ret = true;
        }

    }


    delete m_IniFile;
    return ret;
}
extern "C" CFG_EXPORT bool CheckUser(QString user)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value;
    qDebug() << user;

    //user 三级权限
    for(int i=0;i<3;i++)
    {

        QVariant var;
        key = QString("LEVEL%1").arg(i);
        var =m_IniFile->value("User/"+key);
        if(var.isNull())
            break;
        value = m_IniFile->value("User/"+key).toString().trimmed();
        qDebug() << value;
        QStringList list = value.split(0x04,Qt::SkipEmptyParts);
        QString a,b;
        for(int i = 0;i< list.count();i++){
            QStringList uList = QString(list.at(i)).split(0x02,Qt::SkipEmptyParts);
            a = QString(uList.at(0));
            b = a.mid(1,a.length()-1);
            qDebug() << a << b;
            if(b==user){
                ret = true;
                break;
            }

        }


    }


    delete m_IniFile;
    return ret;
}

extern "C" CFG_EXPORT bool AddUser(QString sLevel,QString user,QString pwd)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value;
    key.append(0x01);
    key.append(user);
    key.append(0x02);
    key.append(pwd);
    key.append(0x03);
    //user 三级权限
    //for(int i=0;i<3;i++)
    do{

        QVariant var;
        var =m_IniFile->value("User/"+sLevel);
        if(var.isNull())
            break;
        value = m_IniFile->value("User/"+sLevel).toString().trimmed();
        qDebug() << value;
        if(value.indexOf(key)>=0){
            ret = true;
        }
        else if(CheckUser(user)==false){
            qDebug() << "44444444444";
            value.append(0x04);
            value.append(key);
            var = QVariant(value);
            m_IniFile->setValue("User/"+sLevel,var);
            ret = true;
            m_IniFile->sync();
            break;
        }

    }while(false);


    delete m_IniFile;
    return ret;
}
extern "C" CFG_EXPORT bool DelUser(QString user)
{
    int ret = false;
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value,kuse;

    int pos=-1,npos = -1;
    //user 三级权限
    for(int i=0;i<3;i++){

        QVariant var;
        key = QString("LEVEL%1").arg(i);
        var =m_IniFile->value("User/"+key);
        if(var.isNull())
            break;
        value = m_IniFile->value("User/"+key).toString().trimmed();
        kuse.append(0x01);
        kuse.append(user);
        kuse.append(0x02);
        pos = value.indexOf(kuse);
        if(pos>=0){
            npos = value.indexOf(0x04,pos);
            if(npos<0) npos = value.length() - pos;

            value.replace(pos,npos,"");
            var = QVariant(value);
            m_IniFile->setValue("User/"+key,var);
            m_IniFile->sync();
            ret = true;
        }

    }


    delete m_IniFile;
    return ret;
}

//extern "C" CFG_EXPORT bool GetUser(QString sLevel,QString &user)
extern "C" CFG_EXPORT std::string GetUser(QString sLevel)
{
    std::string ret = "";
    QString mFolderPath = QApplication::applicationDirPath();
    QDir dir(mFolderPath);
    mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\\"转为"/"
    mFolderPath.append("/SysConfig/Manager.ini");
    QSettings *m_IniFile = new QSettings(mFolderPath, QSettings::IniFormat);

    QString key,value;
    qDebug() << "22222222";

    //user 三级权限
    //for(int i=0;i<3;i++)
    do{

        QVariant var;
        var =m_IniFile->value("User/"+sLevel);
        if(var.isNull())
            break;
        value = m_IniFile->value("User/"+sLevel).toString().trimmed();
        qDebug() << value;
        QStringList list = value.split(0x04);
        QString a,b;
        for(int i = 0;i< list.count();i++){
            QStringList uList = QString(list.at(i)).split(0x02);
            a = QString(uList.at(0));
            b = a.mid(1,a.length()-1);
            qDebug() << a << b;

            ret.append(b.toStdString());
            ret.append(",");
        }


    }while(false);


    delete m_IniFile;
    return std::move(ret);
}
